﻿using System.Linq;
using ExtendedMvc.Ajax;
using ExtendedMvc.Tests.Model;
using ExtendedMvc.Tests.Model.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ExtendedMvc.Tests {


	/// <summary>
	///This is a test class for ResetTest and is intended
	///to contain all ResetTest Unit Tests
	///</summary>
	[TestClass()]
	public class ResetTest {

		private static DummyDataSource database;
		private static ModelViewPage<Dummy> view;
		private static ModelViewForm<Dummy> form;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		public static void MyClassInitialize(TestContext testContext) {
			database = new DummyDataSource();
			var dummy = database.Dummies.All().First();
			view = MvcMockHelpers.FakeViewPage("Index", dummy);
			form = new ModelViewForm<Dummy>(view.Html);
		}

		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup() {
		//}
		//
		//Use TestInitialize to run code before running each test
		[TestInitialize()]
		public void MyTestInitialize() {
			view.ViewContext.ViewData.ModelState.Clear();
		}

		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup() {
		//}
		//
		#endregion

		/// <summary>
		///A test for Reset Constructor
		///</summary>
		[TestMethod()]
		public void ResetButton_WithContentOnly() {
			// Arrange
			var buttonContent = "Reset Text";
			var onClickEvent = string.Format(JavaScriptHelper.Reset, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<button onclick=""{1}"" type=""reset"">{0}</button>", buttonContent, onClickEvent);

			// Act
			var reset = form.ResetButton(buttonContent);
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Reset Constructor
		///</summary>
		[TestMethod()]
		public void ResetButton_WithButtonId() {
			// Arrange
			var buttonContent = "Reset Text";
			var buttonId = "buttonId";
			var onClickEvent = string.Format(JavaScriptHelper.Reset, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<button id=""{2}"" onclick=""{1}"" type=""reset"">{0}</button>", buttonContent, onClickEvent, buttonId);

			// Act
			var reset = form.ResetButton(buttonContent).Id(buttonId);
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Reset Constructor
		///</summary>
		[TestMethod()]
		public void ResetButton_Disabled() {
			// Arrange
			var buttonContent = "Reset Text";
			var onClickEvent = string.Format(JavaScriptHelper.Reset, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<button disabled=""disabled"" type=""reset"">{0}</button>", buttonContent, onClickEvent);

			// Act
			var reset = form.ResetButton(buttonContent).Disabled();
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for ResetLink Constructor
		///</summary>
		[TestMethod()]
		public void ResetLink_ContentOnly() {
			// Arrange
			var informedContent = "ResetLink Text";
			var onClickEvent = string.Format(JavaScriptHelper.Reset, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<a href=""{1}"" type=""reset"">{0}</a>", informedContent, onClickEvent);

			// Act
			var reset = form.ResetLink(informedContent);
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for ResetLink Constructor
		///</summary>
		[TestMethod()]
		public void ResetLink_WithId() {
			// Arrange
			var informedContent = "ResetLink Text";
			var imageId = "imageId";
			var onClickEvent = string.Format(JavaScriptHelper.Reset, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<a href=""{2}"" id=""{1}"" type=""reset"">{0}</a>", informedContent, imageId, onClickEvent);

			// Act
			var reset = form.ResetLink(informedContent).Id(imageId);
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for ResetLink Constructor
		///</summary>
		[TestMethod()]
		public void ResetLink_Disabled() {
			// Arrange
			var informedContent = "ResetLink Text";
			var onClickEvent = "javascript: return false;";
			var expectedOutput = string.Format(@"<a disabled=""disabled"" href=""{1}"" type=""reset"">{0}</a>", informedContent, onClickEvent);

			// Act
			var reset = form.ResetLink(informedContent).Disabled();
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for ResetImage Constructor
		///</summary>
		[TestMethod()]
		public void ResetImage_ContentOnly() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var outputSource = "/images/dummy.jpg";
			var onClickEvent = string.Format(JavaScriptHelper.Reset, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<image onclick=""{1}"" src=""{0}"" type=""reset"" />", outputSource, onClickEvent);

			// Act
			var reset = form.ResetImage(informedSource);
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for ResetImage Constructor
		///</summary>
		[TestMethod()]
		public void ResetImage_WithId() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var outputSource = "/images/dummy.jpg";
			var imageId = "imageId";
			var onClickEvent = string.Format(JavaScriptHelper.Reset, form.Id).Replace("'", "&#39;");
			var expectedOutput = string.Format(@"<image id=""{1}"" onclick=""{2}"" src=""{0}"" type=""reset"" />", outputSource, imageId, onClickEvent);

			// Act
			var reset = form.ResetImage(informedSource).Id(imageId);
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for ResetImage Constructor
		///</summary>
		[TestMethod()]
		public void ResetImage_Disabled() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var outputSource = "/images/dummy.jpg";
			var onClickEvent = "javascript: return false;";
			var expectedOutput = string.Format(@"<image disabled=""disabled"" onclick=""{1}"" src=""{0}"" type=""reset"" />", outputSource, onClickEvent);

			// Act
			var reset = form.ResetImage(informedSource).Disabled();
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for ResetImage Constructor
		///</summary>
		[TestMethod()]
		public void ResetImage_Disabled_WithAlternateImage() {
			// Arrange
			var informedSource = "~/images/dummy.jpg";
			var informedDisabledSource = "~/images/dummy_off.jpg";
			var outputDisabledSource = "/images/dummy_off.jpg";
			var onClickEvent = "javascript: return false;";
			var expectedOutput = string.Format(@"<image disabled=""disabled"" onclick=""{1}"" src=""{0}"" type=""reset"" />", outputDisabledSource, onClickEvent);

			// Act
			var reset = form.ResetImage(informedSource).DisabledImage(informedDisabledSource).Disabled();
			var output = reset.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}
	}
}
